package com.zzg.controller;

import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.zzg.author.model.domain.SysResource;
import com.zzg.author.model.domain.SysRole;
import com.zzg.author.service.SysResourceService;
import com.zzg.author.shrio.service.ShiroService;
import com.zzg.base.response.BaseResponse;
import com.zzg.util.result.ResultUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "资源", description = "资源管理")
@Controller
@RequestMapping("/resource")
@Slf4j
public class ResourceController {
	@Autowired
	private SysResourceService sysResourceService;
	@Autowired
	private ShiroService shiroService;

	/***
	 * 资源列表
	 * 
	 * @return
	 */
	@ApiOperation(value = "资源列表", notes = "资源列表")
	@RequiresPermissions("resources")
	@GetMapping("/resource")
	public ModelAndView resource() {
		return ResultUtil.view("admin/resource/resource");
	}

	@RequiresPermissions("resource:list")
	@GetMapping("/list")
	@ResponseBody
	public List<SysResource> list(SysResource sysResource) {
		List<SysResource> resourceList = sysResourceService.selectResourceList(sysResource);
		return resourceList;
	}

	/**
	 * 加载角色资源列表树
	 * 
	 * @param sysRole
	 * @return
	 */
	@ApiOperation(value = "加载角色资源列表树", notes = "加载角色资源列表树")
	@GetMapping("/roleResourceTreeData")
	@ResponseBody
	public List<Map<String, Object>> roleResourceTreeData(SysRole sysRole) {
		List<Map<String, Object>> trees = sysResourceService.roleResourceTreeData(sysRole);
		return trees;
	}

	/***
	 * 新增资源
	 * 
	 * @return
	 */
	@ApiOperation(value = "新增资源", notes = "新增资源")
	@GetMapping("/add/{id}")
	public ModelAndView add(@PathVariable("id") Integer id, ModelMap map) {
		SysResource resource = null;
		if (id > 0) {
			resource = sysResourceService.selectResourceById(id);
		} else {
			resource = new SysResource();
			resource.setId(0);
			resource.setName("主目录");
		}
		map.put("resource", resource);
		return ResultUtil.view("admin/resource/add");
	}

	@RequiresPermissions("resource:add")
	@PostMapping("/add")
	@ResponseBody
	public BaseResponse add(SysResource sysResource) {
		sysResource.setCreatetime(DateUtil.date());
		sysResource.setUpdatetime(DateUtil.date());
		sysResource.setResourceId(RandomUtil.randomUUID().substring(0, 7).toString());
		if (sysResourceService.insert(sysResource)) {
			shiroService.updatePermission();
			return ResultUtil.success("添加资源成功");
		} else {
			return ResultUtil.error("添加资源失败");
		}
	}

	/***
	 * 根据资源id获取资源数据
	 * 
	 * @return
	 */
	@ApiOperation(value = "根据资源id获取资源数据", notes = "根据资源id获取资源数据")
	@GetMapping("/selectResourceById/{resourceId}")
	public ModelAndView selectResourceById(@PathVariable("resourceId") Integer resourceId, ModelMap map) {
		map.put("resource", sysResourceService.selectResourceById(resourceId));
		return ResultUtil.view("admin/resource/tree");
	}

	/***
	 * 获取资源数据
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取资源数据", notes = "获取资源数据")
	@GetMapping("/resourceTreeAll")
	@ResponseBody
	public List<Map<String, Object>> resourceTreeAll() {
		List<Map<String, Object>> trees = sysResourceService.resourceTreeAll();
		return trees;
	}

	/***
	 * 修改资源
	 * 
	 * @return
	 */
	@ApiOperation(value = "修改资源", notes = "修改资源")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Integer id, ModelMap map) {
		map.put("resource", sysResourceService.selectResourceById(id));
		return ResultUtil.view("admin/resource/edit");
	}

	@RequiresPermissions("resource:edit")
	@PostMapping("/edit")
	@ResponseBody
	public BaseResponse edit(SysResource sysResource) {
		sysResource.setUpdatetime(DateUtil.date());
		if (sysResourceService.updateById(sysResource)) {
			shiroService.updatePermission();
			return ResultUtil.success("修改资源成功");
		} else {
			return ResultUtil.error("修改资源失败");
		}
	}

	/***
	 * 删除资源
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除资源", notes = "删除资源")
	@RequiresPermissions("resource:delete")
	@PostMapping("/delete/{id}")
	@ResponseBody
	public BaseResponse delete(@PathVariable("id") Integer id) {
		if (id <= 1017) {
			return ResultUtil.error("系统资源，请不要删除！");
		}
		if (sysResourceService.selectSubPermsById(id) > 0) {
			return ResultUtil.error("当前资源存在下级资源，无法删除！");
		}
		if (sysResourceService.deleteById(id)) {
			return ResultUtil.success("删除资源成功");
		} else {
			return ResultUtil.error("删除资源失败");
		}
	}
}
